package com.yuanzi.base.mybatis;

import javax.annotation.PreDestroy;
import javax.sql.DataSource;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * 数据源配置
 * @author chengshx
 * @date 2017年5月16日
 */
@Configuration
@MapperScan(basePackages = {"com.yuanzi.*.domain"})
public class MyBatisDataSource {
	
	private DruidDataSource dataSource = null;
	
	@Bean(destroyMethod = "close", name = "dataSource")
	@ConfigurationProperties(prefix="spring.datasource")
	public DataSource dataSource(){
		dataSource = new DruidDataSource();
		return dataSource;
	}
	
	@PreDestroy
	public void close() {
		if(dataSource != null){
			dataSource.close();
		}
	}
}
